package com.yhsimon.realm.member.utils;

import com.yhsimon.realm.member.vo.news.NewsVo;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class NewsCrawlUtils {

    private final static String BD_NEWS_HTML_URL = "https://top.baidu.com/board?tab=realtime";
    private final static String BD_URL = "https://www.baidu.com";
    private final static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0";

    /**
     * 将html保存
     *
     * @param urlString
     * @param filePath
     * @throws IOException
     */
    public static void saveHtml(String urlString, String filePath) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
             FileOutputStream out = new FileOutputStream(filePath)) {
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
        } finally {
            connection.disconnect();
        }
    }


    // TODO 后续可能不保存新闻的源文件，直接存储到数据库中,项目关闭的时候可能文件会丢失【Java -jar的形式运行】
    public static void doSaveHtml() throws IOException {
        LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH"));
        String curDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH"));
        String filePath = "./realm-member/src/main/resources/static/news-" + curDateTime + ".html"; // 指定HTML文件保存位置和文件名
        saveHtml(BD_NEWS_HTML_URL, filePath);
        log.info("HTML saved to {}", filePath);
    }

    /**
     * 获取Web document
     *
     * @param url
     */
    public static Document getWebDocument(String url) {
        try {
            Document document = Jsoup.connect(url)
                    .ignoreContentType(true)
                    .userAgent(USER_AGENT)
                    .timeout(30000)
                    .header("referer", BD_URL)
                    .get();
            return document;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 生成新闻数据
     *
     * @throws Exception
     */
    public static List<NewsVo> generateNewsData() throws Exception {
        // TODO 测试环境先不开器爬取新闻数据 以防IP被封
        // doSaveHtml();
        // String filePath = "./realm-member/src/main/resources/static/news-2024-04-27-00.html";
        // File file = new File(filePath);
        // Document document = Jsoup.parse(file, "UTF-8");
        Document document = getWebDocument(BD_NEWS_HTML_URL);
        List<NewsVo> newsVoList = new ArrayList<>();
        if (document != null) {
            Elements elements = document.getElementsByClass("category-wrap_iQLoo");
            int size = elements.size();
            log.info("热点新闻共有: {}", size);
            for (Element element : elements) {
                String title = element.getElementsByClass("c-single-text-ellipsis").first().text();
                String desc = element.getElementsByClass("hot-desc_1m_jR").first().text();
                String newsUrl = element.getElementsByClass("img-wrapper_29V76").first().attr("href");
                String imageUrl = element.select("img").first().attr("src");
                if (!"http".equals(imageUrl.substring(0, 4))) {
                    imageUrl = element.select("img").get(1).attr("src");
                }
                String newsPopularityIndex = element.getElementsByClass("hot-index_1Bl1a").text();
                log.info("title: {}", title);
                log.info("desc: {}", desc);
                log.info("newsUrl: {}", newsUrl);
                log.info("imageUrl: {}", imageUrl);
                log.info("newsPopularityIndex: {}", newsPopularityIndex);
                log.info("================================================================");

                // 构建NewsVo类
                NewsVo newsVo = NewsVo.builder()
                        .title(title)
                        .desc(desc)
                        .newsUrl(newsUrl)
                        .imageUrl(imageUrl)
                        .newsPopularityIndex(newsPopularityIndex)
                        .build();
                newsVoList.add(newsVo);
            }
        }
        return newsVoList;
    }


}
